package com.hlkj.baojin.controller;

import com.hlkj.baojin.entity.User;
import com.hlkj.baojin.entity.dto.CompanyDto;
import com.hlkj.baojin.entity.dto.CompanySearchDto;
import com.hlkj.baojin.jwt.JwtUser;
import com.hlkj.baojin.service.CompanyService;
import com.hlkj.baojin.service.IUserService;
import com.hlkj.baojin.utils.ValidationUtil;
import com.hlkj.baojin.utils.bean.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author HULIN
 * @created with: baojin
 * @package com.hlkj.baojin.controller
 * @description:
 * @date 2018/12/17 12:48
 * @modified By:
 * @Copyright © 2018 HAILIANG Info. Tech Ltd. All rights reserved.
 */
@Api(value = "/bjjk", description = "公司管理")
@RestController
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "公司列表", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyDto.class))
    @PostMapping(value = "/company/list")
    public ResponseEntity<JsonResult> companyList(@RequestBody CompanySearchDto companySearchDto) {
        return ResponseEntity.ok(JsonResult.success(companyService.findCompany(companySearchDto)));
    }

    @ApiOperation(value = "公司保存", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/company/save")
    public ResponseEntity<JsonResult> companySave(@RequestBody CompanyDto companyDto) {
        ValidationUtil.validate(companyDto);
        companyService.saveCompany(companyDto);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "公司更新", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/company/update")
    public ResponseEntity<JsonResult> companyUpdate(@RequestBody CompanyDto companyDto) {
        companyService.updateCompany(companyDto);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "公司删除", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/company/delete/{companyId}")
    public ResponseEntity<JsonResult> companyDelete(@PathVariable("companyId") String companyId) {
        companyService.deleteCompany(companyId);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "公司查看-后台", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyDto.class))
    @GetMapping(value = "/company/get/{companyId}")
    public ResponseEntity<JsonResult> companyGet(@PathVariable("companyId") String companyId) {
        return ResponseEntity.ok(JsonResult.success(companyService.findCompanyById(companyId)));
    }


    @ApiOperation(value = "公司查看-公司", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyDto.class))
    @GetMapping(value = "/company/get/self")
    public ResponseEntity<JsonResult> companyGet() {
        JwtUser currentUser = userService.getCurrentUser();
        User user = userService.getUserDao().findById(currentUser.getId()).get();
        return ResponseEntity.ok(JsonResult.success(companyService.findCompanyById(user.getDeptId())));
    }

    @ApiOperation(value = "公司下拉列表", tags = {"公司管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = CompanyDto.class))
    @GetMapping(value = "/company/get/list")
    public ResponseEntity<JsonResult> companyList() {
        return ResponseEntity.ok(JsonResult.success(companyService.getCompanyList()));
    }
}